import { Component, OnInit, OnDestroy, Input, OnChanges } from '@angular/core';

import { DashboradMonthHistoryService } from './dashborad.month.history.service';

import { Util } from '../../../../utils/util';

import { Scope } from '../../../../utils/emit';

import { AppSettings } from '../../../../app.settings';

@Component({
  selector: 'dashborad-month-history',
  templateUrl: './dashborad.month.history.html',
  providers: [DashboradMonthHistoryService]
})

export class DashboradMonthHistory {

  @Input('date') date: any;

  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['id', 'date', 'categoryId', 'categoryName', 'categoryLevel', 'monthSellMoney', 'monthSellAmount', 'productNum', 'shopNum', 'monthTsellMoney', 'monthTsellAmount', 'tproductNum', 'tshopNum', 'monthThkSellMoney', 'monthThkSellAmount', 'thkProductNum', 'thkShopNum', 'monthGsellMoney', 'monthGsellAmount', 'gproductNum', 'gShopNum', 'monthCsellMoney', 'monthCsellAmount', 'cproductNum', 'cshopNum'],
      dimText: ['id', '时间', '类目id', '类目', '类目等级', '月销售额', '月销售量', '动销宝贝数', '动销店铺数', '天猫月销售额', '天猫月销售量', '天猫动销宝贝数', '天猫动销店铺数', '天猫国际月销售额', '天猫国际月销售量', '天猫国际动销宝贝数', '天猫国际动销店铺数', '全球购月销售额', '全球购月销售量', '全球购动销宝贝数', '全球购动销店铺数', '淘宝月销售额', '淘宝月销售量', '淘宝动销宝贝数', '淘宝动销店铺数']
    },
    "type": 'line-month'
  };

  columns = ['时间', '月销售量', '月销售额', '动销店铺数', '动销宝贝数'];

  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };

  constructor(private service: DashboradMonthHistoryService, private util: Util, private scope: Scope, private appSettings: AppSettings) {
    // 如果不是第一次进入
    this.cat = this.appSettings.getAppCat();


  }

  sellEvent: any;
  ngOnInit() {


    console.log(this.cat);

    this.sellEvent = this.scope.on('dashborad-all-cur-cat', (data) => {

      console.log("dash sell");
      console.log(data);

      this.cat = data.data;
      this.vm.loading = true;
      this.getHttp(this.cat, this.date.date);

    });

  }

  ngOnChanges(changes) {

    console.log(changes);

    // 日期
    if (changes.date && changes.date.currentValue && changes.date.currentValue.timeType == 'month') {

      console.log(this.date);



      if (this.cat.categoryId == "") {
        console.log('cat not exist');
        console.log(this.cat);
        return;
      }

      // this.getHttp(this.cat,this.date.date);
      this.vm.loading = true;

      this.getHttp(this.cat, this.date.date);


    }




  }



  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();

    }

    if (this.sellEvent) {
      this.sellEvent.unsubscribe();
    }
  }

  // 导出
  export() {
    console.log("export");

    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);

    this.util.export(data, this.vm.column.fileName);
  }



  monthSellResulet: any = {
    monthSellAmount: 0,
    monthSellMoney: 0
  };
  offEvent: any;
  getHttp(cat, date) {



    this.vm.column.fileName = this.cat.wholeName + "销售月走势";

    this.monthSellResulet = {
      monthSellAmount: 0,
      monthSellMoney: 0
    };

    var res = { "code": 0, "msg": "成功", "data": [{ "id": "50019780\u00012017-11\u0001", "date": "2017-11", "crawlDate": 1510329600000, "categoryId": 50019780, "categoryName": "数码/手机/家电--平板电脑/MID", "level1CategoryId": -10081378955521, "categoryLevel": 2, "brandName": "", "brandParition": null, "monthSellAmount": 253281, "monthTsellAmount": 197691, "monthCsellAmount": 54445, "monthGsellAmount": 1078, "monthThkSellAmount": 67, "monthSellMoney": 4.1840342341E8, "monthTsellMoney": 3.0923227177E8, "monthCsellMoney": 1.0609537214E8, "monthThkSellMoney": 22713.0, "monthGsellMoney": 3053066.5, "productNum": 3272, "tproductNum": 1167, "cproductNum": 2076, "thkProductNum": 1, "gproductNum": 28, "shopNum": 752, "tshopNum": 185, "cshopNum": 556, "thkShopNum": 1, "gShopNum": 10 }] };

    setTimeout(() => {

      this.monthSellResulet = this.util.getSellAllResult(this.monthSellResulet, res.data);

      this.vm.data = res.data;

      this.vm.loading = false;

    }, 100);


  }
}
